local skel = fk.CreateSkill {
  name = "emo__yuxing",
}

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic,trick",
  prompt = function (self, player, selected_cards, selected)
    local basic, trick = player:getMark("emo__yuxing_basic-turn") == 0, player:getMark("emo__yuxing_trick-turn") == 0
    if basic and not trick then
      return "#emo__yuxing-basic"
    elseif not basic and trick then
      return "#emo__yuxing-trick"
    end
    return "#emo__yuxing"
  end,
  interaction = function (self, player)
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card.type ~= Card.TypeEquip and not card.is_derived and not player:prohibitUse(card)
      and player:getMark("emo__yuxing_"..card.type.."-turn") == 0 then
        if (Fk.currentResponsePattern == nil and player:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
          table.insertIfNeed(names, card.name)
        end
      end
    end
    if #names == 0 then return end
    return UI.ComboBox {choices = names}
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and self.interaction.data and Fk:currentRoom():getCardArea(to_select) ~= Player.Equip
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:setPlayerMark(player, "emo__yuxing_"..use.card.type.."-turn", 1)
  end,
  enabled_at_play = function(self, player)
    return not player:isKongcheng() and (player:getMark("emo__yuxing_basic-turn") == 0 or player:getMark("emo__yuxing_trick-turn") == 0)
  end,
  enabled_at_response = function(self, player, response)
    if not player:isKongcheng() and Fk.currentResponsePattern then
      for _, id in ipairs(Fk:getAllCardIds()) do
        local card = Fk:getCardById(id)
        if card.type ~= Card.TypeEquip and not card.is_derived and not player:prohibitUse(card)
        and player:getMark("emo__yuxing_"..card.type.."-turn") == 0 then
          if Exppattern:Parse(Fk.currentResponsePattern):match(card) then
            return true
          end
        end
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "emo__yuxing_basic-turn", 0)
  player.room:setPlayerMark(player, "emo__yuxing_trick-turn", 0)
end)

Fk:loadTranslationTable{
  ["emo__yuxing"] = "愉行",
  [":emo__yuxing"] = "每回合每种类别限一次，可以将一张手牌当任意基本牌或锦囊牌使用或打出。",

  ["#emo__yuxing"] = "愉行:可以将一张手牌当任意基本牌或锦囊牌使用或打出",
  ["#emo__yuxing-trick"] = "愉行:可以将一张手牌当任意锦囊牌使用或打出",
  ["#emo__yuxing-basic"] = "愉行:可以将一张手牌当任意基本牌使用或打出",
}


return skel
